<?php
/**
 * V7 checked 1
 * 
 * @author Leon
 */
class data_reader_addition extends data {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 
	 * @param type $uid
	 * @param type $type
	 * @param type $article
	 * @return array (
	 *	show_type		=> "follow"
	 *	follower_id
	 *	circle			=> array (id, title, privacy...)
	 *	articles'		=> array ((id, article_id, user_id, url_id, title, dt, url, user_nickname), ...)
	 * 当show_type == join时，返回数据如下
	 *	show_type		=> 'join',
	 *	inviter_id
	 *	inviter_nickname
	 *	accepter_id
	 *	circle			=> array (同上)
	 *	articles		=> array (同上)
	 * )
	 */
	public function addition_info ($uid, $type, $article) {
		$uid = intval($uid);
		
		if ($uid and is_array($article) and $article['article_id']) {
			switch ($type) {
				case 'circle':
					return $this->info_by_circle($uid, $article);
					break;
				case 'recommend':
					return $this->info_by_recommend($uid, $article);
					break;
				case 'feed':
					return $this->info_by_feed($uid, $article);
					break;
				case 'hot':
					return $this->info_by_hot($uid, $article);
					break;
				default:
					return false;
			}
		} else return false;
	}
	/**
	 * 当reader中打开文章的方式为circle时，即从circle article中读出的记录，则article array中包含有该文章的circle id记录，
	 * 则检查用户与该circle的关系，如果不是成员也没有订阅，并且该circle也有阅读权限，则在reader中提示“订阅此圈子”，并列出圈子的详情和其中的5篇文章
	 * 
	 * @param int $uid
	 * @param Array $article
	 * @return Array
	 */
	private function info_by_circle ($uid, $article) {
		if ($article['circle_id']) {
			$data_circle = new data_circle();
			if ($data_circle->get_access($uid, $article['circle_id']) === false) {		// 不是成员，也没有订阅
				// 显示“加订阅”
				$circle = $data_circle->one($uid, $article['circle_id']);
				if ($circle) {
					$data_save_article = new data_save_article();
					$circle_article = $data_save_article->ls_in_circle($circle['id'], 1, 5);
					if ($circle_article) {
						return array (
							'show_type'		=> 'follow',
							'follower_id'	=> $uid,
							'circle'		=> $circle,
							'articles'		=> $circle_article
						);
					} else return false;
				} else return false;
			} else return false;
		} else return false;
	}
	/**
	 * 当reader中打开文章的方式为recommend时，检索出文章所属的所有圈子，
	 * 检查用户是否是这些圈子的成员，如果不是成员，
	 * 那么在上述圈子中获取级别最高的一个圈子，允许是private的（排序规则：作者在圈子中的地位更高，作者在圈子中发布的文章数更多）
	 * 
	 * @param int $uid
	 * @param Array $article
	 * @return array
	 */
	private function info_by_recommend ($uid, $article) {
		$article_circles = $this->db->execute('
			select
				circle_id
			from
				circle_article
			where
				article_id	= "'.$article['article_id'].'"
		');
		if (is_array($article_circles)) {
			foreach ($article_circles as $v) {
				$article_circles_str .= $v['circle_id'].',';
			}
			$article_circles_str = substr($article_circles_str, 0, -1);
			$in_my_circle = $this->db->execute('
				select
					id
				from
					circle_member
				where
					user_id	= "'.$uid.'"				and
					(type = "member" or type = "admin" or type = "founder")	and
					circle_id	IN ('.$article_circles_str.')
				limit
					1
			');
			if (!$in_my_circle) {
				$circle = $this->db->execute('
					select
						circle_id
					from
						circle_member
					where
						user_id	= "'.$article['user_id'].'"		and
						(type = "member" or type = "admin" or type = "founder")	and
						circle_id	IN ('.$article_circles_str.')
					order by
						type				DESC,
						total_articles_by_me	DESC
					limit
						1
				');
				if ($circle) {
					// 显示“加入”
					$data_circle = new data_circle();
					$circle = $data_circle->one($article['user_id'], $circle[0]['circle_id']);		// 这里要用作者的身份去读circle，否则private的读不出来
					$data_save_article = new data_save_article();
					$circle_article = $data_save_article->ls_in_circle($circle['id'], 1, 5);
					$data_user = new data_user();
					$inviter_nickname = $data_user->friend_to_nickname($uid, $article['user_id']);

					return array (
						'show_type'			=> 'join',
						'inviter_id'			=> $article['user_id'],
						'inviter_nickname'	=> $inviter_nickname,
						'accepter_id'		=> $uid,
						'circle'			=> $circle,
						'articles'			=> $circle_article
					);
				} else return false;
			} else return false;
		} else return false;
	}
	/**
	 * 当reader中打开文章的方式为feed时，检索出文章所属的所有圈子，
	 * 检查用户是否是这些圈子的成员，如果不是成员，
	 * 那么在上述圈子中获取文章数最多的一个public圈子
	 * 
	 * @param int $uid
	 * @param array $article
	 * @return array
	 */
	private function info_by_feed ($uid, $article) {
		$article_circles = $this->db->execute('
			select
				circle_id
			from
				circle_article
			where
				article_id	= "'.$article['article_id'].'"
		');
		if (is_array($article_circles)) {
			foreach ($article_circles as $v) {
				$article_circles_str .= $v['circle_id'].',';
			}
			$article_circles_str = substr($article_circles_str, 0, -1);
			$in_my_circle = $this->db->execute('
				select
					id
				from
					circle_member
				where
					user_id	= "'.$uid.'"				and
					circle_id	IN ('.$article_circles_str.')
				limit
					1
			');
			if (!$in_my_circle) {
				$circle = $this->db->execute('
					select
						*
					from
						circle
					where
						id		IN ('.$article_circles_str.')	and
						privacy	= "public"
					order by
						total_article DESC
					limit
						1
				');
				if ($circle) {
					// 显示“加订阅”
					$circle = $circle[0];
					$data_save_article = new data_save_article();
					$circle_article = $data_save_article->ls_in_circle($circle['id'], 1, 5);

					if ($circle and $circle_article) {
						return array (
							'show_type'		=> 'follow',
							'follower_id'	=> $uid,
							'circle'		=> $circle,
							'articles'		=> $circle_article
						);
					} else return false;

				} else return false;
			} else return false;
		} else return false;
	}
	/**
	 * 当reader中打开文章的方式为hot时，同feed方式
	 * 
	 * @param int $uid
	 * @param array $article
	 * @return array
	 */
	private function info_by_hot ($uid, $article) {
		return $this->info_by_feed($uid, $article);
	}
}
?>
